{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_ml():\n",
    "    os.chdir('/home/tinker/bbserverless/DIC/MachineLearning/')\n",
    "    current_path = os.getcwd()\n",
    "    print(current_path)\n",
    "    path = os.path.join(current_path, \"Inference-of-ml-openwhisk\")\n",
    "    for root, dirs, files in os.walk(path, topdown=False):\n",
    "        for m in dirs:\n",
    "            os.chdir(os.path.join(path, m))\n",
    "            shell = os.path.join(path, m, 'action_update.sh')\n",
    "            r = os.popen('bash '+shell).read()\n",
    "            print(r)\n",
    "            \n",
    "update_ml()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_stream():\n",
    "    os.chdir('/home/tinker/bbserverless/DIC/Stream')\n",
    "    current_path = os.getcwd()\n",
    "    \n",
    "    path = os.path.join(current_path, \"openwhisk\")\n",
    "    os.chdir(path)\n",
    "    print(os.getcwd())\n",
    "    shell = 'action_update.sh'\n",
    "    r = os.popen('bash '+shell).read()\n",
    "    print(r)\n",
    "\n",
    "update_stream()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_bigdata():\n",
    "    os.chdir('/home/tinker/bbserverless/DIC/BigData')\n",
    "    current_path = os.getcwd()\n",
    "    \n",
    "    path = os.path.join(current_path, \"openwhisk\")\n",
    "    os.chdir(path)\n",
    "    print(os.getcwd())\n",
    "    shell = 'action-update.sh'\n",
    "    r = os.popen('bash '+shell).read()\n",
    "    print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/tinker/bbserverless/DIC/BigData/openwhisk\n",
      "ok: updated action bigdata\n",
      "\n"
     ]
    }
   ],
   "source": [
    "update_bigdata()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def update_web():\n",
    "    os.chdir('/home/tinker/bbserverless/DIC/WebServices/openwhisk/')\n",
    "    current_path = os.getcwd()\n",
    "    print(current_path)\n",
    "    path = os.path.join(current_path, \"python-code\")\n",
    "    for root, dirs, files in os.walk(path, topdown=False):\n",
    "        for m in dirs:\n",
    "            if m.find('ipynb')!=-1:\n",
    "                continue\n",
    "            os.chdir(os.path.join(path, m))\n",
    "            shell = os.path.join(path, m, 'action-update.sh')\n",
    "            r = os.popen('bash '+shell).read()\n",
    "            print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "update_web()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wsk -i action list "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"startTime\": 1634280718.5768194,\n",
      "    \"token\": {\n",
      "        \"greeting\": \"Hello from helper.py, stranger!\"\n",
      "    }\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280718.6423109,\n",
      "    \"token\": \"-2417038638172269946\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280718.710165,\n",
      "    \"token\": \"b'gAAAAABhaSUOOptwfFrqudPyRs3lkS0K9jmFa8PrBAWn8U5AXMmq8QvsudiKY5dPpMVGFEvBlexm7l5Mz2z-Gmw1Xkgml8YVtQ=='\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280718.7788734,\n",
      "    \"token\": \"04b463d267c4f68ead02c3bdd44fe64c\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280758.7557852,\n",
      "    \"token\": \"[0, 1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 10, 20, 34, 88, 2232]\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280764.249116,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"error\": \"The action did not produce a valid response and exited unexpectedly.\"\n",
      "}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"startTime\": 1634280771.1224072,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280771.2043145,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280771.2898076,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280771.9151108,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280771.9943864,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280772.0774388,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280775.666978,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n",
      "{\n",
      "    \"startTime\": 1634280785.464326,\n",
      "    \"token\": \"inference finished\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "action_names = ['hello-python', 'hash-python', 'cryptography-python', 'md5-python', 'sort-python', 'facerecognition-general', 'multinomialnb-general',\n",
    "                'passiveaggressiveclassifier-general', 'perceptron-general', 'randomforestregressor-general', 'sgdclassifier-general', 'sgdregressor-general', 'svr-general', 'bigdata', 'stream']\n",
    "for i in action_names:\n",
    "    cmd = 'wsk -i action invoke {function} --result'\n",
    "    os.system(cmd.format(function=i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get pod -n openwhisk"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "9aab921455daf93c5b5e512768862be38eb2d6dcc418fdb7d8b034605503ac32"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
